package code;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class chat extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //人物说的话
        String text=(String)req.getServletContext().getAttribute("text");
        if(text==null) text="";
        System.out.println("*************************");
        String append_text=req.getParameter("text");
        System.out.println(append_text);
        //识别是谁说的，并展示人物和内容
        String name="【"+(String)req.getSession().getAttribute("name")+"】";
        append_text=name+append_text;
        text+=append_text;
        text=text.replace("\n","<br>");
        System.out.println("*************************");
        System.out.println("------------------------");
        req.getServletContext().setAttribute("text",text+"<br>");
        System.out.println((String)req.getServletContext().getAttribute("text"));
        System.out.println("------------------------");
        resp.sendRedirect("chat.jsp");
    }
}